package com.mes.service.hpf;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mes.dao.hpf.SaleReconciliationMapper;
import com.mes.entity.hpf.SaleReconciliation;
import com.mes.entity.yzl.Condition;
import com.mes.entity.yzl.Reconciliation;
import com.mes.entity.yzlExtend.ExtendProductor;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
@Transactional
public class SaleReconciliationServiceImpl implements SaleReconciliationService {

	@Resource
	public SaleReconciliationMapper saleReconciliationMapper;
	
	@Override
	public int reconciliationCount(Condition condition) {
		// TODO Auto-generated method stub
		return saleReconciliationMapper.reconciliationCount(condition);
	}

	@Override
	public List<SaleReconciliation> reconciliationPageList(Condition condition) {
		// TODO Auto-generated method stub
		return saleReconciliationMapper.reconciliationPageList(condition);
	}

	@Override
	public List<ExtendProductor> reconciliationXQ(Condition condition) {
		// TODO Auto-generated method stub
		return saleReconciliationMapper.reconciliationXQ(condition);
	}

	@Override
	public Reconciliation findByDateMonth(Reconciliation reconciliation) {
		// TODO Auto-generated method stub
		return saleReconciliationMapper.findByDateMonth(reconciliation);
	}

	@Override
	public int addReconciliation(Reconciliation reconciliation) {
		// TODO Auto-generated method stub
		return saleReconciliationMapper.addReconciliation(reconciliation);
	}

	@Override
	public int reconciliationAllCount(Condition condition) {
		// TODO Auto-generated method stub
		return saleReconciliationMapper.reconciliationAllCount(condition);
	}

	@Override
	public List<Reconciliation> reconciliationAllPageList(Condition condition) {
		// TODO Auto-generated method stub
		return saleReconciliationMapper.reconciliationAllPageList(condition);
	}

	@Override
	@Transactional
	public int updateReconciliation(JSONArray jsonArray) throws Exception {
		// TODO Auto-generated method stub
		long add = 0;
        int isSuccess = 0;
        int addisSuccess = 0;
        
        for (int i = 0; i < jsonArray.size(); i++) {
        	JSONObject jObj = (JSONObject) jsonArray.get(i);
        	//修改对账信息
            Reconciliation reconciliation = new Reconciliation();
            reconciliation.setId(jObj.getLong("id"));
            reconciliation.setMonthQ(jObj.getInt("monthQ"));
            reconciliation.setMonthP((float)jObj.getDouble("monthP"));
            reconciliation.setMonthSettlementQ(jObj.getInt("monthSettlementQ"));
            reconciliation.setMonthSettlementP((float)jObj.getDouble("monthSettlementP"));
            reconciliation.setClosingBalanceQ(jObj.getInt("closingBalanceQ"));
            reconciliation.setClosingBalanceP((float)jObj.getDouble("closingBalanceP"));
            addisSuccess = saleReconciliationMapper.updateReconciliation(reconciliation);
            if (addisSuccess == 0) {//修改失败  置错误值  跳出循环
            	add = 1;
                break;
            }
        }
        
        if (add == 1) {
            throw new RuntimeException();
        } else {
            isSuccess = 1;
        }
        return isSuccess;
	}

	@Override
	public int resetReconciliation(Reconciliation reconciliation) {
		// TODO Auto-generated method stub
		return saleReconciliationMapper.updateReconciliation(reconciliation);
	}

}
